package com.study.talk.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.android.material.imageview.ShapeableImageView;
import com.study.talk.R;
import com.study.talk.entity.UserInfo;
import com.study.talk.net.OkHttp;

import java.util.List;

public class UserListAdapter extends BaseAdapter {

    List<UserInfo> data;
    private ShapeableImageView userListItemHeadImg;
    private TextView userListItemHeadUsername;
    private TextView userListItemHeadIntroduce;

    public UserListAdapter(List<UserInfo> data) {
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        @SuppressLint({"ViewHolder", "InflateParams"}) View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.user_list_item, null);
        initView(v);
        UserInfo userInfo = data.get(i);
        Glide.with(viewGroup.getContext()).load(OkHttp.URL+userInfo.getAvatar()).into(userListItemHeadImg);
        userListItemHeadUsername.setText(userInfo.getUsername());
        userListItemHeadIntroduce.setText(userInfo.getIntroduce());
        return v;
    }

    private void initView(View view) {
        userListItemHeadImg = view.findViewById(R.id.user_list_item_head_img);
        userListItemHeadUsername = view.findViewById(R.id.user_list_item_head_username);
        userListItemHeadIntroduce = view.findViewById(R.id.user_list_item_head_introduce);
    }
}
